package cn.topcodes.tcsf.wechat.web.controller;

import cn.topcode.unicorn.utils.JsonUtil;
import cn.topcodes.tcsf.admin.commons.api.controller.admin.AdminController;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.wechat.domain.entity.WechatVoice;
import cn.topcodes.tcsf.wechat.domain.form.WechatVoiceForm;
import cn.topcodes.tcsf.wechat.domain.vo.WechatVoiceVo;
import cn.topcodes.tcsf.wechat.service.WechatFileService;
import cn.topcodes.tcsf.wechat.service.WechatVoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * @author xw
 * @date 2017-12-22 15:16:33
 */
@Controller
@RequestMapping("/admin/wechat/voice")
public class WechatVoiceController extends AdminController<WechatVoice, WechatVoiceVo, WechatVoiceForm> {

    @Autowired
    private WechatVoiceService wechatVoiceService;

    @Autowired
    private WechatFileService wechatFileService;

    @Override
    protected String getPageFolder() {
        return "wechat/admin/material/voice";
    }

    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public String upload(
            @RequestParam String mpId,
            MultipartFile file) {
        String voiceSavePath = wechatFileService.getVoiceStoragePath();
        String name = file.getOriginalFilename();

        File voiceFile = new File(voiceSavePath);
        if (!voiceFile.exists()) {
            voiceFile.mkdirs();
        }
        voiceFile = new File(voiceSavePath, name);

        try {
            if (!voiceFile.exists()) {
                voiceFile.createNewFile();
            } else {
                voiceFile =
                        wechatFileService.createFile(voiceSavePath, wechatFileService.getSuffix(name));
            }
            file.transferTo(voiceFile);
            wechatVoiceService.upload(mpId, voiceFile);
        } catch (Exception e) {
            if (voiceFile.exists()) {
                voiceFile.delete();
            }
            e.printStackTrace();
        }
        return "redirect:/admin/wechat/voice";
    }

    @RequestMapping(value = "download")
    public void download(@RequestParam Long id, HttpServletResponse response) throws IOException {
        WechatVoice voice = wechatVoiceService.findOne(id);
        if (voice != null) {
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(wechatFileService.getNameByUrl(voice.getUrl(), "/"), "UTF-8"));
            response.setHeader("Connection", "close");
            response.setHeader("Content-Type", "application/octet-stream");
            wechatFileService.responseFileByName(
                    WechatFileService.FILE_TYPE_VOICE, wechatFileService.getNameByUrl(voice.getUrl(), "/"), response.getOutputStream());
        }
    }

    @Override
    @ResponseBody
    public String update(@RequestParam Long id, HttpServletRequest request, Model model) {
        WechatVoice voice = wechatVoiceService.findOne(id);
        String title = request.getParameter("title");
        voice.setTitle(title);
        wechatVoiceService.update(voice);
        return JsonUtil.toJson(ApiResponseEntity.buildOK());
    }
}
